﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESPL.CodeGenerator.Common.ProjectBuilders;
using ESPL.CodeGenerator.Common;
using ESPL.CodeGenerator.Common.Structs;
using ESPL.CodeGenerator.Domain.TemplateGenerators.SolutionGenerators;
using ESPL.CodeGenerator.Domain.Utilities;
using ESPL.CodeGenerator.Domain.TemplateGenerators.ProjectGenerators;
using ESPL.CodeGenerator.Common.Enums;

namespace ESPL.CodeGenerator.Domain.ProjectBuilders
{
    internal class PresentationProjectBuilder : ProjectBuilderBase, IDataProjectBuilder
    {
        #region Constructor

        public PresentationProjectBuilder(ApplicationConfiguration config, ClassLibraryConfiguration projectConfig)
            : base(config, projectConfig)
        {
            WriteFile = true;
            CreateDir = true;
            _projectConfig.NameSpace = ProjectName;
            var commonReference = string.Format("{0}.{1}.Common", Config.CompanyNameAbbrevation, Config.ApplicationName);
            var domainReference = string.Format("{0}.{1}.Domain", Config.CompanyNameAbbrevation, Config.ApplicationName);
            _projectConfig.References.Add(commonReference);
            _projectConfig.References.Add(domainReference);
            BuildControllers();
        }

        #endregion

        #region Methods

        protected override BuilderResponse GetResponse()
        {
            return new BuilderResponse { Output = string.Format("{0} Project Created at: {1}", _generator.ToString(), GetFilePath()) };
        }

        public override System.Collections.Generic.IList<ESPL.CodeGenerator.Common.IBuilderResponse> InitializeProjectTree()
        {
            var response = base.InitializeProjectTree();
            ProcessEdmxProject("PresentationControllerGenerator.tt");
            return response;
        }

        private void BuildControllers()
        {
            Entities.Add("BaseController.cs");
            foreach (var entity in Config.Entities)
            {
                Entities.Add(string.Format("{0}Controller.cs", entity.Name));
            }
        }

        #endregion
    }
}
